package com.dexgdx.game.handlers;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Vector2;
import com.dexgdx.game.Game;
import com.dexgdx.game.manager.ActorManager;

public class DexInputProcessor implements GestureListener, InputProcessor {
	
	public boolean keyDown(int keycode) {
		return true;
	}

	public boolean keyUp(int keycode) {
		return true;
	}

	public boolean keyTyped(char character) {
		return true;
	}

	public boolean touchDown(int x, int y, int pointer, int button) {
		return true;
	}

	public boolean touchUp(int x, int y, int pointer, int button) {
		if(Game.selectionRectangle.isVisible()){
			Game.selectionRectangle.setInvisible();
		}
		else {
			boolean actorTouched = false;
			for (Long playerIndex : Game.actorData.getPlayerList()) {
				if(ActorManager.getActor(playerIndex).contains(x, y)){
					ActorManager.selectPlayer(playerIndex, false);
					actorTouched = true;
					break;
				}
			}
			if(!actorTouched){
				for (Long enemyIndex : Game.actorData.getEnemyList()) {
					if(ActorManager.getActor(enemyIndex).contains(x, y)){
						ActorManager.selectPlayer(enemyIndex, false);
						actorTouched = true;
						break;
					}
				}
			}
			if(!actorTouched){
				Game.emptyScreenTouched(x, y);
			}
		}
		return true;
	}

	public boolean touchDragged(int x, int y, int pointer) {
		if(Game.selectionRectangle.isVisible()){
			
			if(x < Game.selectionRectangle.getStartPointX()){
				Game.selectionRectangle.x = x;
			}
			else {
				Game.selectionRectangle.x = Game.selectionRectangle.getStartPointX();
			}
			if(y < Game.selectionRectangle.getStartPointY()){
				Game.selectionRectangle.y = y;
			}
			else {
				Game.selectionRectangle.y = Game.selectionRectangle.getStartPointY();
			}
			Game.selectionRectangle.width = Math.abs(Game.selectionRectangle.getStartPointX() - x);
			Game.selectionRectangle.height = Math.abs(Game.selectionRectangle.getStartPointY() - y);
			
			for(Long actorIndex : Game.actorData.getPlayerList()){
				if(Intersector.overlaps(Game.selectionRectangle, ActorManager.getActor(actorIndex))){
					ActorManager.selectPlayer(actorIndex, true);
				}
				else {
					ActorManager.deSelectPlayer(actorIndex);
				}
			}
		}
		return true;
	}

	public boolean mouseMoved(int x, int y) {
		return false;
	}

	public boolean scrolled(int amount) {
		return false;
	}

	@Override
	public boolean touchDown(float x, float y, int pointer, int button) {
		return false;
	}

	@Override
	public boolean tap(float x, float y, int count, int button) {
		return false;
	}

	@Override
	public boolean longPress(float x, float y) {
		Game.selectionRectangle.x = x - 100;
		Game.selectionRectangle.y = y - 100;
		Game.selectionRectangle.width = x - Game.selectionRectangle.x;
		Game.selectionRectangle.height = y - Game.selectionRectangle.y;
		Game.selectionRectangle.setVisible(x - 100, y - 100);
		
		return false;
	}

	@Override
	public boolean fling(float velocityX, float velocityY, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean pan(float x, float y, float deltaX, float deltaY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean panStop(float x, float y, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean zoom(float initialDistance, float distance) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
			Vector2 pointer1, Vector2 pointer2) {
		// TODO Auto-generated method stub
		return false;
	}
}